load("@batfish//skylark:pmd_test.bzl", "pmd_test")
load("@rules_java//java:defs.bzl", "java_library")

package(default_visibility = ["//visibility:public"])

###################################################################
### parser_common_files are files that antlr4-generated
### java code is allowed to depend on. We keep this set small
### and explicit to minimize the number of times we have to
### recompile parsers when we change unrelated things in common.
###
### TODO: actually have a sensible directory structure and modules
###################################################################
parser_common_srcs = [
    "org/batfish/common/BatfishException.java",
    "org/batfish/common/DebugBatfishException.java",
    "org/batfish/common/ErrorDetails.java",
    "org/batfish/common/ParseTreeSentences.java",
    "org/batfish/common/WillNotCommitException.java",
    "org/batfish/common/util/BatfishObjectMapper.java",
    "org/batfish/common/util/serialization/BatfishThirdPartyDeserializers.java",
    "org/batfish/common/util/serialization/BatfishThirdPartySerializationModule.java",
    "org/batfish/common/util/serialization/BatfishThirdPartySerializers.java",
    "org/batfish/common/util/serialization/guava/RangeSetDeserializer.java",
    "org/batfish/common/util/serialization/guava/RangeSetDeserializerConverter.java",
    "org/batfish/common/util/serialization/guava/RangeSetSerializer.java",
    "org/batfish/common/util/serialization/guava/RangeSetSerializerConverter.java",
    "org/batfish/datamodel/answers/AnswerElement.java",
    "org/batfish/datamodel/answers/AnswerSummary.java",
    "org/batfish/grammar/BatfishANTLRErrorStrategy.java",
    "org/batfish/grammar/BatfishCombinedParser.java",
    "org/batfish/grammar/BatfishGrammarErrorListener.java",
    "org/batfish/grammar/BatfishLexer.java",
    "org/batfish/grammar/BatfishLexerErrorListener.java",
    "org/batfish/grammar/BatfishLexerRecoveryStrategy.java",
    "org/batfish/grammar/BatfishParseException.java",
    "org/batfish/grammar/BatfishParseTreeWalker.java",
    "org/batfish/grammar/BatfishParser.java",
    "org/batfish/grammar/BatfishParserATNSimulator.java",
    "org/batfish/grammar/BatfishParserErrorListener.java",
    "org/batfish/grammar/GrammarSettings.java",
    "org/batfish/grammar/ImplementedRules.java",
    "org/batfish/grammar/ParseTreePrettyPrinter.java",
    "org/batfish/grammar/UnrecognizedLineToken.java",
    "org/batfish/grammar/flattener/FlattenerLineMap.java",
]

java_library(
    name = "parser_common",
    srcs = parser_common_srcs,
    deps = [
        "@maven//:com_fasterxml_jackson_core_jackson_annotations",
        "@maven//:com_fasterxml_jackson_core_jackson_core",
        "@maven//:com_fasterxml_jackson_core_jackson_databind",
        "@maven//:com_fasterxml_jackson_datatype_jackson_datatype_guava",
        "@maven//:com_fasterxml_jackson_datatype_jackson_datatype_jdk8",
        "@maven//:com_fasterxml_jackson_datatype_jackson_datatype_jsr310",
        "@maven//:com_google_code_findbugs_jsr305",
        "@maven//:com_google_guava_guava",
        "@maven//:org_antlr_antlr4_runtime",
    ],
)

pmd_test(
    name = "parser_common_pmd",
    lib = ":parser_common",
)

###################################################################
### specifier_common_srcs are files that must be compiled with the
### generated Parboiled parsers used in specifier resolution.
### We must exclude the Parboiled parsers from code coverage, so
### keeping this library small means we get coverage for as much
### of the project as possible.
###
### TODO: actually have a sensible directory structure and modules
### TODO: figure out why bazel coverage + parboiled = crash and fix
###################################################################
specifier_common_srcs = [
    "org/batfish/common/util/CompletionMetadataUtils.java",
    "org/batfish/datamodel/answers/AutoCompleteUtils.java",
    "org/batfish/datamodel/answers/InputValidationUtils.java",
    "org/batfish/datamodel/PacketHeaderConstraints.java",
    "org/batfish/datamodel/PacketHeaderConstraintsUtil.java",
    "org/batfish/datamodel/phc_to_flow/DstIpExtractorDefault.java",
    "org/batfish/datamodel/phc_to_flow/DstIpExtractorPhcOnly.java",
    "org/batfish/datamodel/phc_to_flow/FieldExtractor.java",
    "org/batfish/datamodel/phc_to_flow/IpFieldExtractorContext.java",
    "org/batfish/datamodel/phc_to_flow/IpProtocolExtractorDefaultTcp.java",
    "org/batfish/datamodel/phc_to_flow/SrcIpExtractorDefault.java",
    "org/batfish/datamodel/phc_to_flow/SrcIpExtractorLocation.java",
    "org/batfish/datamodel/questions/BgpPeerPropertySpecifier.java",
    "org/batfish/datamodel/questions/BgpProcessPropertySpecifier.java",
    "org/batfish/datamodel/questions/InterfacePropertySpecifier.java",
    "org/batfish/datamodel/questions/NamedStructurePropertySpecifier.java",
    "org/batfish/datamodel/questions/NodePropertySpecifier.java",
    "org/batfish/datamodel/questions/OspfInterfacePropertySpecifier.java",
    "org/batfish/datamodel/questions/OspfProcessPropertySpecifier.java",
    "org/batfish/datamodel/questions/VxlanVniPropertySpecifier.java",
    "org/batfish/specifier/DispositionSpecifier.java",
    "org/batfish/specifier/Grammar.java",
    "org/batfish/specifier/ReferenceFilterGroupFilterSpecifier.java",
    "org/batfish/specifier/RoutingProtocolSpecifier.java",
    "org/batfish/specifier/SpecifierFactories.java",
] + glob(["org/batfish/specifier/parboiled/*.java"])

java_library(
    name = "specifier_common",
    srcs = specifier_common_srcs,
    deps = [
        ":common_lib",
        ":parser_common",
        "//projects/bdd",
        "@maven//:com_fasterxml_jackson_core_jackson_annotations",
        "@maven//:com_fasterxml_jackson_core_jackson_databind",
        "@maven//:com_google_code_findbugs_jsr305",
        "@maven//:com_google_guava_guava",
        "@maven//:com_google_re2j_re2j",
        "@maven//:org_apache_commons_commons_lang3",
        "@maven//:org_apache_commons_commons_text",
        "@maven//:org_parboiled_parboiled_core",
        "@maven//:org_parboiled_parboiled_java",
    ],
)

pmd_test(
    name = "specifier_common_pmd",
    lib = ":specifier_common",
)

###################################################################
### common_lib is the bulk of the batfish-common-protocol common code.
### It is so cyclicly dependent that it must be compiled as one unit
### rather than using efficient, modular, incremental compilation.
###
### TODO: actually have a sensible directory structure and modules
###################################################################
java_library(
    name = "common_lib",
    srcs = glob(
        [
            "**/*.java",
        ],
        exclude = specifier_common_srcs + parser_common_srcs,
    ),
    plugins = [
        "//:auto_service_plugin",
    ],
    resources = [
        "//projects/batfish-common-protocol/src/generated:common_properties",
        "//projects/batfish-common-protocol/src/main/resources/org/batfish/common/util:salt",
    ],
    runtime_deps = [
        "@maven//:commons_beanutils_commons_beanutils",
        "@maven//:jakarta_activation_jakarta_activation_api",
        "@maven//:org_glassfish_jersey_inject_jersey_hk2",
    ],
    deps = [
        ":parser_common",
        "//projects/bdd",
        "@maven//:at_yawk_lz4_lz4_java",
        "@maven//:com_carrotsearch_hppc",
        "@maven//:com_fasterxml_jackson_core_jackson_annotations",
        "@maven//:com_fasterxml_jackson_core_jackson_core",
        "@maven//:com_fasterxml_jackson_core_jackson_databind",
        "@maven//:com_fasterxml_jackson_datatype_jackson_datatype_guava",
        "@maven//:com_fasterxml_jackson_datatype_jackson_datatype_jdk8",
        "@maven//:com_fasterxml_jackson_datatype_jackson_datatype_jsr310",
        "@maven//:com_github_ben_manes_caffeine_caffeine",
        "@maven//:com_google_auto_service_auto_service_annotations",
        "@maven//:com_google_code_findbugs_jsr305",
        "@maven//:com_google_errorprone_error_prone_annotations",
        "@maven//:com_google_guava_guava",
        "@maven//:com_google_re2j_re2j",
        "@maven//:com_ibm_icu_icu4j",
        "@maven//:commons_cli_commons_cli",
        "@maven//:commons_io_commons_io",
        "@maven//:dk_brics_automaton",
        "@maven//:jakarta_ws_rs_jakarta_ws_rs_api",
        "@maven//:org_antlr_antlr4_runtime",
        "@maven//:org_apache_commons_commons_configuration2",
        "@maven//:org_apache_commons_commons_lang3",
        "@maven//:org_apache_commons_commons_text",
        "@maven//:org_apache_logging_log4j_log4j_api",
        "@maven//:org_codehaus_jettison_jettison",
        "@maven//:org_glassfish_grizzly_grizzly_framework",
        "@maven//:org_glassfish_grizzly_grizzly_http_server",
        "@maven//:org_glassfish_jersey_containers_jersey_container_grizzly2_http",
        "@maven//:org_glassfish_jersey_core_jersey_server",
        "@maven//:org_jgrapht_jgrapht_core",
        "@maven//:org_skyscreamer_jsonassert",
    ],
)

pmd_test(
    name = "common_lib_pmd",
    lib = ":common_lib",
)

###################################################################
### common is a virtual dependency aggregating and exporting the
### submodules in this directory so that downstream code does not
### need to change when the internal module structure changes.
###################################################################
java_library(
    name = "common",
    exports = [
        ":common_lib",
        ":parser_common",
        ":specifier_common",
    ],
)

alias(
    name = "common_testlib",
    actual = "//projects/batfish-common-protocol/src/test:common_testlib",
)
